/*
 * Framework for communicating between the Facebook Connect API and 
 * Adobe Flash via ExternalInterface.
 *
 * @author SplashCast (www.splashcastmedia.com)
 * @author Brock Henderson
 * @version 0.007
 *
 * Distributed under the MIT License
 *
*/

var debug_ref;
var content_ref;
var swf_ref;
var connected = false;
var debugging_on;
var framework_ver = "0.007";

/* Debugging trace function that will print out to a DIV with the ID 'debug_output'
 * @param msg [string] - message to be printed
 * @param o [object] - [optional] one-dimensional object tree whos key/value pairs will be printed
*/
function debugOut(msg, o) {
	if (!debugging_on) return;
	try {
		debug_ref.innerHTML += msg+"<br>";
		if (o != null) {
			for (var x in o) {
				debug_ref.innerHTML += " obj."+x+" : "+o[x]+"<br>";
			}
		}
	}
	catch (e) {}
}

/* Method for writing HTML into the content frame
 * @param input HTML to add to the content frame
*/
function writeContent(input, target_id, overwrite) {
	if (target_id != null) {
		try {
			if (overwrite) document.getElementById(target_id).innerHTML = input;
			else document.getElementById(target_id).innerHTML += input;
		}
		catch(e) {
			debugOut("Failed to write HTML to target ["+target_id+"]");
		}
	}
	else {
		if (overwrite) content_ref.innerHTML = input;
		else content_ref.innerHTML += input;
	}
}

/* Gets an object reference for a OBJECT/EMBED combo Flash SWF
 * @param name [string] - ID/NAME of the OBJECT/EMBED
 * @returns reference to the SWF object
*/
function getSWF(name) {
	var isIE = window.navigator.appName.indexOf("Microsoft") != -1;
 	return (isIE) ? window[name] : document[name];
}

/* Create connections to global div references 
 * Externalized this so I could startup debugging before embed is written
*/
function makeConnections(target_id) {
	// setup debugging
	try {
		debug_ref = document.getElementById("debug_output");
		debugging_on = true;
	}
	catch (e) {
		debugging_on = false;
	}
	// setup content frame (let JS throw error if id not found)
	content_ref = document.getElementById(target_id);
	connected = true;
}

/* Writes the SWF OBJECT/EMBED into the innerHTML of the target_id element
 * @param url [string] - web address of the SWF file
 * @param w [string] - width value to be written into the elements
 * @param h [string] - height value to be written into the elements
 * @param target_id [string] - ID of the element to write the HTML into
 * @returns - string value of embed if target_id is null or boolean value 'true' if the function successfully executes
*/
function writeEmbed(url,w,h,swf_id,target_id) {
	if (!connected) makeConnections(target_id);
   if (debugging_on) debugOut("myspace-opensocial-flash.js version: "+framework_ver+"<br />writeEmbed()");
	// build embed str
	var fvar_str = "";
	try {
		if (fvars) {
			for (var x in fvars) fvar_str += x+"="+fvars[x]+"&";
			fvar_str += "brwsr_ver="+getBrowserVersion();
			if (debugging_on) debugOut("FlashVars constructed:\n["+fvar_str+"]");
		}
		fvar_str += window.location.search.slice(1);
	}
	catch (e) {
		fvars = false;
	}
	var embed_str = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"";
	embed_str += "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\"";
	embed_str += " width=\""+w+"\"";
	embed_str += " height=\""+h+"\"";
	embed_str += " id=\""+swf_id+"\">";
	embed_str += "<param name=\"allowScriptAccess\" value=\"always\" />";
	embed_str += "<param name=\"allowNetworking\" value=\"all\" />";
	embed_str += "<param name=\"wmode\" value=\"opaque\" />";
	embed_str += "<param name=\"movie\" value=\""+url+"\" />";
	if (fvars) embed_str += "<param name=\"flashvars\" value=\""+fvar_str+"\" />";
	embed_str += "<embed type=\"application/x-shockwave-flash\"";
	embed_str += " allowScriptAccess=\"always\"";
	embed_str += " allowNetworking=\"all\"";
	embed_str += " wmode=\"opaque\"";
	embed_str += " src=\""+url+"\"";
	embed_str += " width=\""+w+"\"";
	embed_str += " height=\""+h+"\"";
	if (fvars) embed_str += " flashvars=\""+fvar_str+"\"";
	embed_str += " name=\""+swf_id+"\">";
	embed_str += "</embed></object>";
	// write embed to target element or return as string
	if (target_id != null) {
		if (!connected) writeContent(embed_str, target_id, true);
		else writeContent(embed_str, null, true);
		swf_ref = getSWF(swf_id);
		if (debugging_on) debugOut("SWF refrerence aquired");
		return true;
	}
	else return embed_str;
}

/* Gets Browser Info
 * @returns string Browser name & version
*/
function getBrowserVersion() {
	return navigator.appName+" "+navigator.appVersion; //userAgent
}
